import re

text = "小王的邮箱是: kwang@mail.com, 电话: 188-1234-5678。还有一个邮箱是：abc_xyz@company.org, 另外电话: 139-8765-4321。"
reg = r"\w+@{1}\w+\.[a-zA-Z]+"
match_list = re.findall(reg, text)

new_text = re.sub(reg, "111111111111", text)
print(new_text)

reg_tel = r"(\d{3,4}-\d{3,4}-\d{3,4})"
tel_list = re.findall(reg_tel, text)
print(tel_list)


# 使用sub进行全局替换，类似js的replace函数
new_text = re.sub(reg_tel, r"086-\1", text)
print(new_text)


# match函数使用. match：只检查是否从开头匹配
result = re.match(r"小王的邮箱是", text)
if result:
    print("match - 匹配到字符串开头:", result.group())
else:
    print("match - 没有匹配到字符串开头")


# 2. search：查找第一个邮箱
email = re.search(r"\w+@\w+\.\w+", text)
if email:
    print("search - 第一个邮箱:", email.group())

# 3. finditer：遍历所有邮箱并打印位置信息
print("finditer - 所有邮箱位置和内容:")
for match in re.finditer(r"\w+@\w+\.\w+", text):
    print(f"邮箱: {match.group()}，起始: {match.start()}，结束: {match.end()}")

match_all_group = re.finditer(reg, text)

print(type(match_all_group), "match_all_group=", match_all_group)

for idx, match in enumerate(match_all_group):
    print(f"邮箱: {match.group()}，起始: {match.start()}，结束: {match.end()}")


# 7. match.group()用法示例：
m = re.match(r"(\w+)的邮箱是: (\w+@\w+\.\w+)", text)
if m:
    print("match.group(0):", m.group(0))  # 整体
    print("match.group(1):", m.group(1))  # "小王"
    print("match.group(2):", m.group(2))  # 邮箱
